﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Newtonsoft.Json;
using System.Web.Services;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.Exam
{
    public partial class BmKCList : BasePage
    {
        private string CurPageCode = "KC";
        private string CurPage = "BmKCList.aspx";
        private string QCPageCode = "QC";
        private string QCPage = "BmQCList.aspx";
        private string KMPageCode = "KM";
        private string KMPage = "BmKMList.aspx";
        private string AppID
        {
            get
            {
                return HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////绑定期次下拉
                BindQCDDL();
                ////绑定科目下拉
                BindKMDDL();
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            ////绑定数据源
            BindData();
        }

        protected void ddlQC_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindKMDDL();
            RadGrid1.CurrentPageIndex = 0;
            BindData();
            RadGrid1.DataBind();
        }

        protected void ddlKM_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadGrid1.CurrentPageIndex = 0;
            BindData();
            RadGrid1.DataBind();
        }

        private void BindQCDDL()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                ddlQC.DataSource = JsonConvert.DeserializeObject<List<ModelService.Exam_QC>>(client.GetExamQCList
                    (QCPageCode, UserInfo.RoleId, QCPage, AppID, 0, 0, out count));
                ddlQC.DataValueField = "ID";
                ddlQC.DataTextField = "Title";
                ddlQC.DataBind();
                ddlQC.Items.Insert(0, new ListItem("--请选择--", ""));
            }
        }

        private void BindKMDDL()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                ddlKM.DataSource = JsonConvert.DeserializeObject<List<ModelService.Exam_KM>>(client.GetExamKMList
                    (KMPageCode, UserInfo.RoleId, KMPage, AppID, ddlQC.SelectedValue, 0, 0, out count));
                ddlKM.DataValueField = "ID";
                ddlKM.DataTextField = "Title";
                ddlKM.DataBind();
                ddlKM.Items.Insert(0, new ListItem("--请选择--", ""));
            }
        }

        /// <summary>
        /// 绑定考场列表
        /// </summary>
        private void BindData()
        {
            RadGrid1.AllowCustomPaging = true;
            int pageCount = RadGrid1.CurrentPageIndex;
            int pageSize = RadGrid1.PageSize;
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                ////获得考场列表，分appid
                var exam = JsonConvert.DeserializeObject<List<ModelService.Exam_Room>>(client.GetExamKCList(
                    CurPageCode, 
                    UserInfo.RoleId, 
                    AppID, 
                    ddlQC.SelectedValue, 
                    ddlKM.SelectedValue, 
                    CurPage,
                    pageCount + 1, 
                    pageSize, 
                    out count));
                if (exam != null)
                {
                    RadGrid1.VirtualItemCount = count;
                    RadGrid1.DataSource = exam;
                }
            }
        }

        [WebMethod]
        public static string DelKC(object id)
        {
            ////删除考场
            string ret = null;
            int kcId = 0;
            int.TryParse(id.ToString(), out kcId);
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                OperationResult result = new OperationResult();
                result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.DeleteExamKC(kcId));
                ret = JsonConvert.SerializeObject(new { Message = result.Message });
            }
            return ret;
        }

        /// <summary>
        /// 根据期次id获取期次Title
        /// </summary>
        /// <param name="qcId"></param>
        /// <returns></returns>
        public string GetQCTitle(object qcId)
        {
            string qc = string.Empty;
            int id = 0;
            int.TryParse(qcId.ToString(), out id);
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                var exam = JsonConvert.DeserializeObject<ModelService.Exam_QC>(client.GetExamQC(id));
                qc = exam.Title;
            }
            return qc;
        }

        /// <summary>
        /// 根据科目id获取科目Title
        /// </summary>
        /// <param name="kmId"></param>
        /// <returns></returns>
        public string GetKMTitle(object kmId)
        {
            string km = string.Empty;
            int id = 0;
            int.TryParse(kmId.ToString(), out id);
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                var exam = JsonConvert.DeserializeObject<ModelService.Exam_KM>(client.GetExamKM(id));
                km = exam.Title;
            }
            return km;
        }
    }
}